package m.store.action;

import m.base.action.BaseAction;
import m.base.util.LevelUtil;
import m.base.util.PowerUtil;
import m.common.action.VueResult;
import m.common.action.ActionMeta;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.list.*;
import m.store.model.GoodsRecordNotice;
import m.store.model.OrderRepositoryCheck;
import m.store.service.GoodsRecordNoticeService;
import m.store.util.ColorUtil;
import m.system.exception.MException;
import m.system.util.JSONMessage;

@ActionMeta(name = "store/goodsRecordNotice")
public class GoodsRecordNoticeAction extends BaseAction {
    private GoodsRecordNotice model;
    public JSONMessage doRead() throws Exception {
        getService(GoodsRecordNoticeService.class).readNotice(model);
        return JSONMessage.success("操作成功");
    }
    protected ListColumn toTypeColumn(){
        String dict="m.store.model.GoodsRecordNotice!type";
        return toElementColumn("类型", "type", 100,
                ListElement.tag(dict, ColorUtil.getNoticeType(),false)
        ).setAlign(Align.CENTER).setFilter(true).setDictType(dict);
    }
    protected ListColumn toStatusColumn(){
        String dict="m.store.model.GoodsRecordNotice!status";
        return toElementColumn("状态", "status", 100,
            ListElement.dot(dict, ColorUtil.getNoticeStatus())
        ).setAlign(Align.CENTER).setFilter(true).setDictType(dict);
    }
    /*
     * 商品退货通知列表
     */
    public VueResult goodsRefundList() throws Exception {
        VueListResult result=getVueListResult("商品退货通知", GoodsRecordNotice.class,actionUrl("goodsRefundList"));
        result.setFunction(LevelUtil.exportFunc(),false);
        result.setHeight(-1);
        result.setSelection("oid",false);
        result.setSelectionDisabled("record.status!='"+GoodsRecordNotice.Status_Notice+"'");
        result.setMergeCellsInfo(1,5);
        result.addCol(toIndexColumn());
        result.addCol(toLinkColumn("订单单号", "orderNo", 100, true,
            new ListEvent(actionUrl(OrderModelAction.class,"toViewOrder"), EventType.MODAL,1200)
                .addParam("oid","orderOid").addParam("orderNo","orderNo")
        ));
        result.addCol(toDateColumn("订单账期", "orderDate", 100, true));
        result.addCol(toTextColumn("录入用户", "inputUser.realname", 100, true));
        result.addCol(toTextColumn("客户", "guest.name", 130, true));
        result.addCol(toTypeColumn());
        result.addCol(toTextColumn("仓库", "repository.name", 100, true));
        result.addCol(toTextColumn("商品名称", "goods.name", 100, true));
        result.addCol(toNumberColumn("单价", "price", 100, true).setGroupTitle("退货"));
        result.addCol(toIntegerColumn("数量", "num", 100, true).setGroupTitle("退货"));
        result.addCol(toNumberColumn("总优惠", "preferential", 100, true).setGroupTitle("退货"));
        result.addCol(toNumberColumn("金额", "amount", 100, true).setGroupTitle("优惠后"));
        result.addCol(toNumberColumn("单价", "preferentialPrice", 100, true).setGroupTitle("优惠后"));
        result.addCol(toTextColumn("备注", "remark", 100, true));
        result.addCol(toDateColumn("操作时间", "operDate", 100, true));
        result.addCol(toStatusColumn().setFixed(Align.RIGHT));
        result.addCol(toSpaceColumn("操作", 100,
            ListElement.test("record.status=='"+ GoodsRecordNotice.Status_Notice+"'",
                ListButton.ajax("审阅完成", ButtonType.PRIMARY,actionUrl("doRead"), SuccessMethod.REFRESH)
                    .addParam("model.oid","oid").setConfirm("确定已阅吗？")
            )
        ).setFixed(Align.RIGHT));
        result.addButton(
            ListButton.ajax("批量已阅", ButtonType.PRIMARY,actionUrl("doRead"), SuccessMethod.REFRESH)
                .addParam("model.oid","oid").setConfirm("确定要批量已阅这些记录吗？")
        );
        result.addCondition(QueryCondition.eq("noticeUser.oid", PowerUtil.getSessionUserOid()));
        result.addCondition(QueryCondition.in("type",GoodsRecordNotice.Type_Refund_Sale,GoodsRecordNotice.Type_Refund_Stock));
        result.addOrder(
            QueryOrder.desc("orderDate")
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        return result;
    }

    public GoodsRecordNotice getModel() {
        return model;
    }

    public void setModel(GoodsRecordNotice model) {
        this.model = model;
    }
}
